package com.xinfan.tank;

import java.io.*;
import java.util.Vector;

/**
 * @Author xin
 * @Date 2022/6/9 18:57
 * @Version 1.0
 *
 * 用于记录击毁数量
 * 文件写出
 */
public class Recorder {
    //均设为静态的相当于一个工具类
    private static int count;

    private static BufferedWriter bw = null;

    private static BufferedReader br = null;

    private static String path = "src\\record.txt";

    private static Vector<EnemyTank> enemyTanks = null;

    //存放节点信息
    private static Vector<Node> nodes = new Vector<>();

    /*public static void setNodes(Vector<Node> nodes) {
        Recorder.nodes = nodes;
    }*/

    public static String getPath() {
        return path;
    }

    public static Vector<Node> getNodes() {
        try {
            br = new BufferedReader(new FileReader(path));
            count = Integer.parseInt(br.readLine());
            String line;
            while ((line = br.readLine()) != null) {
                String[] xyd = line.split(" ");
                Node node = new Node(Integer.parseInt(xyd[0]), Integer.parseInt(xyd[1]), Integer.parseInt(xyd[2]));
                nodes.add(node);

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return nodes;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static void saveRecord() {
        try {
            FileWriter fileWriter = new FileWriter(path);
            bw = new BufferedWriter(fileWriter);
            bw.write(count+"\r\n");
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                bw.write(enemyTank.getX()+" "+enemyTank.getY()+" "+enemyTank.getDirection());
                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();//!!!!!!!!!!!!!!!!关流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static int getCount() {
        return count;
    }

    public static void setCount(int count) {
        Recorder.count = count;
    }

    public static void addCount() {
        count++;
    }
}
